package com.asa.playiconview

import android.animation.AnimatorSet
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat

class PlayIconView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {


    companion object{
        const val  DURATION = 1800L
        const val PLAY_STATUS = 1   //播放动画状态
        const val WAIT_STATUS = 2   //停止动画状态
    }

    private var columnarTop = 0f
    private var columnarBottom = 0f
    private var valueAnimatorTop1 = 0f
    private var valueAnimatorTop2 = 0f
    private var valueAnimatorTop3 = 0f
    private var status = PLAY_STATUS
    private var diameter = 0f  //直径
    private var columnarSpan = 0f
    private var columnarWidth = 0f
    private var columnarDefaultHeight = 44f
    private var rect = RectF()
    private val paint = Paint()
    private val animatorSet = AnimatorSet()

    init {
        paint.isDither = true
        paint.isAntiAlias = true
        columnarDefaultHeight = context.resources.getDimension(R.dimen.dp_54)
        columnarWidth = context.resources.getDimension(R.dimen.dp_32)
        val valueAttrs = context.obtainStyledAttributes(attrs,R.styleable.PlayIconView)
        paint.color= valueAttrs.getColor(R.styleable.PlayIconView_columnarColor,ContextCompat.getColor(context,R.color.black))
        columnarDefaultHeight = valueAttrs.getDimension(R.styleable.PlayIconView_columnarHeight,columnarDefaultHeight)
        columnarWidth = valueAttrs.getDimension(R.styleable.PlayIconView_columnarWidth,columnarWidth)
        columnarSpan = valueAttrs.getDimension(R.styleable.PlayIconView_columnarSpan,columnarSpan)
        valueAttrs.recycle()

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val widthSize = MeasureSpec.getSize(widthMeasureSpec)
        val heightSize = MeasureSpec.getSize(heightMeasureSpec)
        val maxSize = Math.max(widthSize, heightSize)
        setMeasuredDimension(maxSize, maxSize)
        diameter= maxSize.toFloat()
        columnarTop = maxSize * 0.24f
        columnarBottom = maxSize * 0.76f
        initColumnarAnim()
    }

    private fun initColumnarAnim() {
        val minHeight= columnarBottom * 0.8f
        val animationValue1 = ValueAnimator.ofFloat(minHeight,columnarTop)
        animationValue1.repeatMode = ValueAnimator.REVERSE
        animationValue1.repeatCount = ValueAnimator.INFINITE


        val animationValue2 = animationValue1.clone()
        val animationValue3 = animationValue1.clone()

        animationValue3.addUpdateListener { valueAnimator->
            valueAnimatorTop1 = animationValue1.animatedValue as Float
            valueAnimatorTop2 = animationValue2.animatedValue as Float
            valueAnimatorTop3 = animationValue3.animatedValue as Float
            invalidate()
        }

        animationValue2.startDelay = DURATION /2
        animationValue3.startDelay = DURATION


        animatorSet.playTogether(animationValue1,animationValue2,animationValue3)
        animatorSet.duration = DURATION
        animatorSet.start()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (status == PLAY_STATUS){
            drawColumnar(canvas)
        }
    }

    private fun drawColumnar(canvas: Canvas) {
        var left = diameter /2f - columnarSpan - columnarWidth - columnarWidth/2f
        var top = 0f
        var right = left + columnarWidth
        var bottom = columnarBottom
        for (i in 1..3){
            top = getTop(i)
            rect.set(left,top,right,bottom)
            canvas.drawRoundRect(rect,4f,4f,paint)
            left += columnarWidth + columnarSpan
            right  = left + columnarWidth
        }
    }

    fun getTop(i:Int):Float{
        return when(i){
            1->valueAnimatorTop1
            2->valueAnimatorTop2
            3->valueAnimatorTop3
            else->columnarDefaultHeight
        }
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        if (status == PLAY_STATUS){
            animatorSet.resume()
            animatorSet.start()
        }else{
            animatorSet.pause()
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        animatorSet.pause()
    }

}